package com.wfcm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wfcm.annotation.IgnoreToken;
import com.wfcm.datasource.DynamicDataSource;
import com.wfcm.entity.WfChapterRecordEntity;
import com.wfcm.entity.WfCourseChapterEntity;
import com.wfcm.entity.WfCourseInfoEntity;
import com.wfcm.entity.WfVipCardEntity;
import com.wfcm.service.WfChapterRecordService;
import com.wfcm.service.WfCourseChapterService;
import com.wfcm.service.WfCourseCommentService;
import com.wfcm.service.WfCourseInfoService;
import com.wfcm.service.WfVipCardService;
import com.wfcm.utils.R;

/**
 * 
 * @content 课程章节Controller
 */
@RequestMapping("/iosCourse")
@Controller
public class IosCourseController {
	private Logger log = LoggerFactory.getLogger(AdInfoController.class);
	@Autowired
	WfCourseChapterService courseChapterService;
	@Autowired
	WfCourseInfoService courseInfoService;
	@Autowired
	private WfVipCardService vipCardService;
	@Autowired
	private WfCourseCommentService courseCommentService;
	@Autowired
	private WfChapterRecordService chapterRecordService;
	
	/**
	 * 课程信息接口
	 */
	@RequestMapping("/courseInfo")
	@ResponseBody
	@IgnoreToken
	public R courseInfo(HttpServletRequest req) {
		String courseId = req.getParameter("courseId");
		log.debug("courseId:"+courseId);
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("courseId", Integer.valueOf(courseId));
		
		WfCourseInfoEntity  courseInfoEntity = courseInfoService.queryObject(Integer.valueOf(courseId));
		List<WfCourseChapterEntity> chapterList = courseChapterService.queryList(map);

		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap.put("courseInfoEntity", courseInfoEntity);
		resultMap.put("chapterlist", chapterList);
		return R.ok(resultMap);
	}
	
	/**
	 * 章节信息接口
	 */
	@RequestMapping("/courseChapter")
	@ResponseBody
	@IgnoreToken
	public R courseChapter(HttpServletRequest req) {
		int id = Integer.valueOf(req.getParameter("chapterId"));
		log.debug("chapterId:"+id);
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("chapterId", id);
		
		//查询播放次数
		WfChapterRecordEntity chapterRecord = chapterRecordService.queryObject(id);
		int playTotal = 0;
		if(null != chapterRecord) {
			playTotal = chapterRecord.getNumber();
		}
		
		//查询是否点赞

		map.put("commentType", 1);//1.点赞 2.评论
		int greatTotal = courseCommentService.queryTotal(map);//点赞总数
		map.put("commentType", 2);
		int commentTotal = courseCommentService.queryTotal(map);//评论总数
		WfCourseChapterEntity courseChapterEntity = courseChapterService.queryChapter(id);
		
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap.put("courseChapterEntity", courseChapterEntity);
		resultMap.put("greatTotal", greatTotal);
		resultMap.put("commentTotal", commentTotal);
		resultMap.put("playTotal", playTotal);
		return R.ok(resultMap);
	}
	
	/**
	 * 课程包信息接口
	 */
	@RequestMapping("/vipCard")
	@ResponseBody
	@IgnoreToken
	public R vipCourse(HttpServletRequest req) {
		
		int id = Integer.valueOf(req.getParameter("vipCardId"));
		log.debug("vipCardId:"+id);
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("vipCardId", id);
		
		WfVipCardEntity  vipCardEntity = vipCardService.queryObject(id);
		List<WfVipCardEntity>  vipteacherList = vipCardService.queryTeacherList(map);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap.put("vipCardEntity", vipCardEntity);
		resultMap.put("vipteacherList", vipteacherList);
		return R.ok(resultMap);
	}
	
}
